home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWViews / FWButton.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  9.1 KB  |  321 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWButton.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWBUTTON_H
  13. #include "FWButton.h"
  14. #endif
  15.  
  16. // ----- Framewrk Includes -----
  17.  
  18. #ifndef FWFRAME_H
  19. #include "FWFrame.h"
  20. #endif
  21.  
  22. #ifndef FWPART_H
  23. #include "FWPart.h"
  24. #endif
  25.  
  26. #ifndef FWCONTXT_H
  27. #include "FWContxt.h"
  28. #endif
  29.  
  30. // ----- Foundation Includes -----
  31.  
  32. #ifndef FWSTREAM_H
  33. #include "FWStream.h"
  34. #endif
  35.  
  36. #ifndef FWRRECT_H
  37. #include "FWRRcShp.h"
  38. #endif
  39.  
  40. #ifndef FWNOTDEF_H
  41. #include "FWNotDef.h"
  42. #endif
  43.  
  44. // ----- OpenDoc Includes -----
  45.  
  46. #ifndef SOM_ODSession_xh
  47. #include <ODSessn.xh>
  48. #endif
  49.  
  50. #ifndef SOM_ODFacet_xh
  51. #include <Facet.xh>
  52. #endif
  53.  
  54. // ----- Macintosh Includes -----
  55.  
  56. #if defined(FW_BUILD_MAC) && !defined(__CONTROLS__)
  57. #include <Controls.h>
  58. #endif
  59.  
  60. //========================================================================================
  61. // File scope definitions
  62. //========================================================================================
  63.  
  64. #ifdef FW_BUILD_MAC
  65. #pragma segment fwgadgts
  66. #endif
  67.  
  68. //========================================================================================
  69. // CLASS FW_CButton
  70. //========================================================================================
  71.  
  72. FW_DEFINE_CLASS_M1(FW_CButton, FW_CNativeControl)
  73. FW_DEFINE_AUTO(FW_CButton)
  74.  
  75. // This class is archivable, but we provide the archiving implementation in a separate
  76. // translation unit in order to enable deadstripping of the archiving-related code
  77. // in parts that do not use archiving with this class.
  78.  
  79. //----------------------------------------------------------------------------------------
  80. // FW_CButton::FW_CButton
  81. //----------------------------------------------------------------------------------------
  82.  
  83. FW_CButton::FW_CButton(Environment* ev, 
  84.                            FW_CSuperView* container, 
  85.                         const FW_CRect& bounds,
  86.                            ODID viewID,
  87.                         FW_ButtonKind kind,  
  88.                         const FW_CString& label,
  89.                         const FW_CFont& font,
  90.                          FW_Message msg,
  91.                          FW_ControlValue value) :
  92.     FW_CNativeControl(ev, container, bounds, viewID,  msg, value),
  93.     fKind(0)
  94. {
  95.     Initialize(ev, kind, label, font);
  96.     FW_END_CONSTRUCTOR
  97. }
  98.  
  99. //----------------------------------------------------------------------------------------
  100. //    FW_CButton::FW_CButton
  101. //----------------------------------------------------------------------------------------
  102.  
  103. FW_CButton::FW_CButton(Environment* ev) :
  104.     FW_CNativeControl(ev),
  105.     fKind(0)
  106. {    
  107.     FW_END_CONSTRUCTOR
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // FW_CButton::~FW_CButton
  112. //----------------------------------------------------------------------------------------
  113.  
  114. FW_CButton::~FW_CButton()
  115. {
  116.     FW_START_DESTRUCTOR
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // FW_CButton::MacGetProcID
  121. //----------------------------------------------------------------------------------------
  122. #ifdef FW_BUILD_MAC
  123.  
  124. short FW_CButton::MacGetProcID() 
  125. {
  126.     short procID = pushButProc;    // use this as default
  127.     
  128.     if (fKind == FW_kRadioButton)        procID = radioButProc;
  129.     else if (fKind == FW_kCheckButton)    procID = checkBoxProc;
  130.     
  131.     return procID;
  132. }
  133. #endif
  134.  
  135. //----------------------------------------------------------------------------------------
  136. // FW_CButton::SetButtonKind
  137. //----------------------------------------------------------------------------------------
  138.  
  139. void FW_CButton::SetButtonKind(Environment* ev, FW_ButtonKind kind)
  140. {
  141. FW_UNUSED(ev);
  142.     // For now this method can only be used to change a push button to a default button
  143.     FW_ASSERT(fKind == FW_kDefaultPushButton || kind == FW_kDefaultPushButton);
  144.  
  145.     // Use a margin to add/remove a default button's outline
  146.     fControlHelper->SetControlMargin(kind == FW_kDefaultPushButton ? 4 : 0);        
  147.     
  148.     fKind = kind;
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // FW_CButton::Draw
  153. //----------------------------------------------------------------------------------------
  154.  
  155. void FW_CButton::Draw(Environment* ev, ODFacet* facet, ODShape* invalidShape)
  156. {
  157. #ifdef FW_BUILD_MAC
  158.     FW_CNativeControl::Draw(ev, facet, invalidShape);
  159.  
  160.     if (fKind == FW_kDefaultPushButton)
  161.     {
  162.         FW_CViewContext gc(ev, this, facet, invalidShape);
  163.         // Draw the standard default button border
  164.         
  165.         FW_Fixed buttonOval = FW_IntToFixed(2) + FW_Half(GetSize(ev).y);
  166.         FW_CRoundRectShape::RenderRoundRect(
  167.             gc,
  168.             FW_CRect(FW_kZeroPoint, GetSize(ev)),
  169.             FW_CPoint(buttonOval, buttonOval),
  170.             FW_kFrame,
  171.             FW_kNormalInk,
  172.             FW_CStyle(FW_IntToFixed(3)));
  173.     }
  174. #endif
  175.  
  176. #ifdef FW_BUILD_WIN
  177.     // Windows handles updating controls automatically
  178. #endif
  179. }
  180.  
  181. //----------------------------------------------------------------------------------------
  182. // FW_CButton::SimulateButtonPressed
  183. //----------------------------------------------------------------------------------------
  184.  
  185. void FW_CButton::SimulateButtonPressed(Environment* ev)
  186. {
  187.     if (fKind == FW_kPushButton || fKind == FW_kDefaultPushButton) {
  188. #ifdef FW_BUILD_MAC
  189.         FW_CViewContext gc(ev, this, GetFrame(ev)->GetActiveFacet(ev), NULL);
  190.     
  191.         FW_CPlatformPoint qdPoint    = gc.LogicalToDevice(FW_kZeroPoint);
  192.         FW_CPlatformPoint qdSize    = GetSize(ev).AsPlatformPoint();
  193.         fControlHelper->SimulateButtonPressed(qdPoint, qdSize);
  194. #endif
  195.     }
  196.     
  197.     // Send notification
  198.     Notify(ev, FW_CControlNotification(this));
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. // FW_CButton::PrivSetValue
  203. //----------------------------------------------------------------------------------------
  204.  
  205. FW_Boolean FW_CButton::PrivSetValue(Environment *ev, FW_ControlValue value, ODFacet* facet)
  206. {
  207.     // Update the native control.  Return false if value was not changed 
  208.     if (FW_CNativeControl::PrivSetValue(ev, value, facet))
  209.     {
  210.         // If it's a radio button being turned on we must notify its radio-cluster receiver
  211.         // This should be done before any custom message the radio button might be sending
  212.         // to other receivers
  213.         if (fKind == FW_kRadioButton && value != 0)
  214.             Notify(ev, FW_CControlNotification(this, FW_kRadioClusterMsg));
  215.             
  216.         // Send a control message to its receivers
  217.         if (GetMessage(ev) != FW_kNullMsg && GetMessage(ev) != FW_kRadioClusterMsg)
  218.             Notify(ev, FW_CControlNotification(this));
  219.  
  220.         return true;
  221.     }
  222.     else
  223.         return false;
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. // FW_CButton::ControlClicked
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void FW_CButton::ControlClicked(Environment *ev, FW_ControlValue oldValue, ODFacet* facet)
  231. {
  232.     // Change radio and checkbox value (this will send a notification too)
  233.     // Send the notification directly for push buttons 
  234.     
  235.     if (GetButtonKind(ev) == FW_kRadioButton)
  236.         PrivSetValue(ev, 1, facet);
  237.     else if (GetButtonKind(ev) == FW_kCheckButton)
  238.         PrivSetValue(ev, 1 - oldValue, facet);
  239.     else
  240.         Notify(ev, FW_CControlNotification(this));
  241. }
  242.  
  243. //----------------------------------------------------------------------------------------
  244. // FW_CButton::Initialize
  245. //----------------------------------------------------------------------------------------
  246.  
  247. void FW_CButton::Initialize(Environment* ev, 
  248.                         FW_ButtonKind kind, 
  249.                         const FW_CString& label,
  250.                         const FW_CFont& font)
  251. {
  252.     short max = 1;
  253.     fKind = kind;    
  254.     
  255.     if (fKind == FW_kPushButton || fKind == FW_kDefaultPushButton)
  256.     {
  257.         fValue = 0;                        // Push Button's value cannot change
  258.         max = 0;
  259.     }
  260.     else
  261.     {
  262.         if (fValue != 0)  fValue = 1;    // Radio and Check buttons have 2 values
  263.     }
  264.  
  265. #ifdef FW_BUILD_MAC
  266.     Str255 pascalText;
  267.     label.ExportPascal(pascalText);
  268.     fControlHelper = new FW_CPrivMacControlHelper
  269.                         (ev, this, fValue, 0, max, MacGetProcID(), pascalText, font);
  270. #endif
  271.  
  272. #ifdef FW_BUILD_WIN
  273.     // TO UPDATE
  274.     fControlHelper  = new FW_CPrivWinControlHelper
  275.                             (ev, FW_TYPEID_FROM_TYPE(FW_CPrivWinControlHelper), this);
  276.     fControlHelper->SetText(label);
  277. #endif
  278.     
  279.     // Control message define Default/Cancel buttons inside a FW_CDialogFrame
  280.     FW_CFrame* frame = GetFrame(ev);
  281.     frame->PrivButtonAttached(ev, this);
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. //    FW_CButton::InitializeFromStream
  286. //----------------------------------------------------------------------------------------
  287.  
  288. void FW_CButton::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  289. {
  290.     FW_CNativeControl::InitializeFromStream(ev, stream);
  291.     
  292.     FW_ButtonKind kind;
  293.     FW_CString label;
  294.     FW_CFont font;
  295.  
  296.     stream >> kind;
  297.     stream >> font;
  298.     stream >> label;
  299.  
  300.     FW_ASSERT(kind >= 0 && kind <= FW_kMaxButtonKind);
  301.     
  302.     Initialize(ev, kind, label, font);
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    FW_CButton::Flatten
  307. //----------------------------------------------------------------------------------------
  308.  
  309. void FW_CButton::Flatten(Environment* ev, FW_CWritableStream& stream) const
  310. {
  311.     FW_CNativeControl::Flatten(ev, stream);
  312.  
  313.     FW_CString title;
  314.     fControlHelper->GetText(title);
  315.     
  316.     stream << fKind;
  317.     stream << title;    
  318.     stream << fControlHelper->GetFont(ev);
  319. }
  320.  
  321.